/*    */ package cn.zzuisa.news.servlet;

import cn.zzuisa.news.entity.Recruit;
/*    */
/*    */ import cn.zzuisa.news.enums.CommonEnums;
/*    */ import cn.zzuisa.news.service.RecruitService;
/*    */ import cn.zzuisa.news.service.impl.RecruitServiceImpl;
/*    */ import cn.zzuisa.news.util.JsonUtil;
/*    */ import cn.zzuisa.news.util.ResultVOUtil;

import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
/*    */ import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/*    */ import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
/*    */ import javax.servlet.annotation.WebServlet;
/*    */ import javax.servlet.http.HttpServlet;
/*    */ import javax.servlet.http.HttpServletRequest;
/*    */ import javax.servlet.http.HttpServletResponse;
/*    */

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

/*    */
/*    */
/*    */ @WebServlet(name = "recruit", urlPatterns = { "/recruit" }, asyncSupported = true)
/*    */ public class ZZURecruitServlet/*    */ extends HttpServlet
/*    */ {
	/* 21 */ RecruitService recruitServiceImpl = new RecruitServiceImpl();
	/*    */
//<!—Servlet  界面直接调用就可以了，这里的小数类型···-->

	// 导出 Excel
	public void getGenerateExcel(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

	/*    */
	/*    */ protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/* 25 */ String method = req.getParameter("method");
		/* 26 */ if (method == null) {
			/* 27 */ req.setAttribute("lists", this.recruitServiceImpl.list(null));
			/* 28 */ req.getRequestDispatcher("WEB-INF/ftl/zzurecruit/recruit.ftl").forward(req, resp);
			/* 29 */ } else if (method.equals("type")) {
			/* 30 */ String key = req.getParameter("key");
			/* 31 */ req.setAttribute("lists", this.recruitServiceImpl.list(key));
			/* 32 */ req.getRequestDispatcher("WEB-INF/ftl/zzurecruit/recruit.ftl").forward(req, resp);
			/* 33 */ }

		else if (method.equals("out")) {
			List<Recruit> list = new ArrayList();
			list = this.recruitServiceImpl.list("all");
			// 第一步，创建一个webbook，对应一个Excel文件
			HSSFWorkbook wb = new HSSFWorkbook();
			// 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
			HSSFSheet sheet = wb.createSheet("iotlab");
			// 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
			HSSFRow row = sheet.createRow((int) 0);
			// 第四步，创建单元格，并设置值表头 设置表头居中
			HSSFCellStyle style = wb.createCellStyle();
			/* style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式 */

			HSSFCell cell = row.createCell(0);
			cell.setCellValue("姓名");
			cell.setCellStyle(style);
			cell = row.createCell(1);
			cell.setCellValue("专业");
			cell.setCellStyle(style);

			for (int i = 0; i < list.size(); i++) {
				row = sheet.createRow((int) i + 1);
				Recruit recruit = (Recruit) list.get(i);
				// 第五步，创建单元格，并设置值

				row.createCell(0).setCellValue(recruit.getName());// 姓名
				row.createCell(1).setCellValue(recruit.getMajor());// 专业

			}
			// 第六步，将文件存到指定位置
			try {
				Date date = new Date();
				String str = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSSS").format(date); // 需要文件导出带有时间的，请把时间拼接到/杠后面去
				str = "iotlab_2019招新";
				resp.setContentType("application/octet-stream;charset=utf-8");
				resp.setHeader("Content-Disposition",
						"attachment;filename=" + new String(str.getBytes(), "iso-8859-1") + ".xls");
				wb.write(resp.getOutputStream());
				// 如果转化为字节流：
				ByteArrayOutputStream os = new ByteArrayOutputStream();
				wb.write(os);
				byte[] bytes = os.toByteArray();
				// 获取响应报文输出流对象
				ServletOutputStream out = resp.getOutputStream();
				// 输出
				out.write(bytes);
				out.flush();
				out.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			/* 33 */ } else if (method.equals("status")) {
			/* 34 */ Integer id = Integer.valueOf(Integer.parseInt(req.getParameter("id")));
			/* 35 */ Integer key = Integer.valueOf(Integer.parseInt(req.getParameter("key")));
			/*    */
			/* 37 */ System.out.println("id:" + id + " key:" + key);
			/* 38 */ if (this.recruitServiceImpl.updateStatus(id, key)) {
				/* 39 */ resp.getWriter().println(JsonUtil.setData(ResultVOUtil.success()));
				/*    */ } else {
				/* 41 */ resp.getWriter().println(JsonUtil.setData(ResultVOUtil.failed(CommonEnums.PARAM_ERROR)));
				/*    */ }
			/*    */ }
		/*    */ }
	/*    */ }

/*
 * Location:
 * /Users/frank/Desktop/admin/WEB-INF/classes/!/cn/zzuisa/news/servlet/
 * ZZURecruitServlet.class Java compiler version: 8 (52.0) JD-Core Version:
 * 1.0.7
 */